﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using com.IronOne.BoardPACWinAppBO.Category;
using com.IronOne.BoardPACWinAppDAO.DbOperations;
using com.IronOne.BoardPACWinAppDAO.DB;
using com.IronOne.BoardPACWinAppUtil.Enums;

namespace com.IronOne.BoardPACWinAppDAO.Category
{
    public class CategoryDAO : ICategoryDAO
    {
        private readonly IDbOperations _dbOperations;

        public CategoryDAO()
        {
            _dbOperations = new DbOperations.DbOperations();
        }

        public async Task<List<CategoryModel>> GetCategories()
        {
            try
            {
                var conn = _dbOperations.GetAsyncConnection(DbConnectionType.UserDbConnetion);

                var data = await conn.Table<MainCategory>().OrderBy(x => x.CategoryOrder).ToListAsync();
                if (data == null || data.Count <= 0)
                    return null;

                return EntityToModelMapper.MapMainCategoryToCategoryModel(data);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<SubCategoryModel>> GetSubCategories()
        {
            try
            {
                var conn = _dbOperations.GetAsyncConnection(DbConnectionType.UserDbConnetion);

                var data = await conn.Table<SubCategory>().OrderBy(x => x.SubCategoryOrder).ToListAsync();
                if (data == null || data.Count <= 0)
                    return null;

                return EntityToModelMapper.MapSubCategoryToSubCategoryModel(data);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<CategoryModel> GetCategoryByCategoryId(int categoryId)
        {
            try
            {
                var conn = _dbOperations.GetAsyncConnection(DbConnectionType.UserDbConnetion);

                var data = await conn.Table<MainCategory>().Where(x => x.CategoryID == categoryId).FirstOrDefaultAsync();
                return data == null ? null : EntityToModelMapper.MapCategoryEntityToModel(data);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<SubCategoryModel> GetSubCategoryBySubCategoryId(int subCategoryId)
        {
            try
            {
                var conn = _dbOperations.GetAsyncConnection(DbConnectionType.UserDbConnetion);

                var data =
                    await conn.Table<SubCategory>().Where(x => x.SubCategoryId == subCategoryId).FirstOrDefaultAsync();
                return data == null ? null : EntityToModelMapper.MapSubCategoryEntityToModel(data);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<SubCategoryModel>> GetSubCategoriesByCategoryId(int categoryId)
        {
            try
            {
                var conn = _dbOperations.GetAsyncConnection(DbConnectionType.UserDbConnetion);

                var data =
                    await
                        conn.Table<SubCategory>()
                            .Where(x => x.CategoryId == categoryId)
                            .OrderBy(y => y.SubCategoryOrder)
                            .ToListAsync();

                if (data == null || data.Count <= 0)
                    return null;

                return EntityToModelMapper.MapSubCategoryToSubCategoryModel(data);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}